<template>
  <div class="goods-list">
    <!-- <router-link
      class="goods-item"
      v-for="item in goodsList"
      :key="item.id"
      :to="'/home/goodsinfo/' + item.id"
      tag="div"
    >
      <div class="goods-img">
        <img :src="item.img_url" alt="图片加载失败" />
      </div>
      <h3 class="goods-title">{{ item.title }}</h3>
      <div class="goods-msg">
        <p class="price">
          <span class="price-now">￥{{ item.sell_price }}</span>
          <span class="price-old">￥{{ item.market_price }}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{ item.stock_quantity }}件</span>
        </p>
      </div>
    </router-link>-->

    <div class="goods-item" v-for="item in goodsList" :key="item.id" @click="getData(item.id)">
      <div class="goods-img">
        <img :src="item.img_url" alt="图片加载失败" />
      </div>
      <h3 class="goods-title">{{ item.title }}</h3>
      <div class="goods-msg">
        <p class="price">
          <span class="price-now">￥{{ item.sell_price }}</span>
          <span class="price-old">￥{{ item.market_price }}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{ item.stock_quantity }}件</span>
        </p>
      </div>
    </div>

    <!-- 加载更多 -->
    <mt-button type="danger" size="large" @click.native="getMore">加载更多</mt-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pageIndex: 1,
      goodsList: []
    };
  },
  methods: {
    // 获取商品列表
    getGoodsList() {
      this.$http
        .get("api/getgoods?pageindex=" + this.pageIndex)
        .then(result => {
          if (result.body.status === 0) {
            // this.goodsList = result.body.message;
            this.goodsList = this.goodsList.concat(result.body.message);
          }
        });
    },
    getMore() {
      this.pageIndex++;
      this.getGoodsList();
    },
    // 编程式导航------------------
    // vue-router 有两种调转的方式
    // 一种是 router-link ， 一种是 编程式导航
    getData(id) {
      // 1. 最简单的
      //   this.$router.push("goodsinfo/" + id);
      // 2. 对象
      //   this.$router.push({ path: "goodsinfo/" + id });
      // 3. 传递命名路由
      this.$router.push({ name: "goodsinfo", params: { id } });
    }
  },
  created() {
    this.getGoodsList();
  }
};
</script>

<style lang="scss">
.goods-list {
  display: flex;
  flex-wrap: wrap;
  padding: 8px;
  justify-content: space-between;
  .goods-item {
    width: 49%;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin-bottom: 8px;
    padding: 2px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 300px;
    h3 {
      font-size: 14px;
      color: black;
    }
    .goods-msg {
      background-color: #eee;
      p {
        margin: 0;
        padding: 3px 4px;
      }
      .price {
        .price-now {
          color: red;
          font-size: 16px;
          font-weight: 700;
        }
        .price-old {
          font-size: 12px;
          text-decoration: line-through;
          margin-left: 10px;
        }
      }
      .sell {
        letter-spacing: 1px;
        font-size: 13px;
        display: flex;
        justify-content: space-between;
      }
    }
    img {
      width: 100%;
    }
  }
}
</style>